<template>
  <div>
    <Card>
      <Collapse style="margin-bottom:15px;">
        <Panel>查询
          <div slot="content">
            <Form
              ref="formLkyCollege"
              :model="formLkyCollege"
              label-position="right"
              :label-width="80"
              inline
            >
              <FormItem label="学院名称" prop="name">
                <Input type="text" v-model="formLkyCollege.name" placeholder="学院名称"/>
              </FormItem>
              <FormItem label="学院编号" prop="collage_number">
                <Input type="text" v-model="formLkyCollege.collage_number" placeholder="学院编号"/>
              </FormItem>
              <FormItem>
                <Button type="primary" @click="handleSubmit('formLkyCollege')">查询</Button>
                <Button @click="handleReset('formLkyCollege')" style="margin-left: 8px">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Panel>
      </Collapse>
      <ButtonGroup style="margin-bottom: 15px;">
        <!--<Button @click="handleOpenPage('add')">添加</Button>-->
        <Add v-model="isOpenAddPage" @addRow="addLkyCollege" />
        <Edit v-model="isOpenEditPage" @updateRow="updateLkyCollege" :lkyCollege="editLkyCollege" />
        <!--<Button @click="batchDelete()">删除</Button>-->
      </ButtonGroup>
      <Table
        ref="tableLkyCollege"
        :data="tableData"
        :columns="columns"
        @on-selection-change="onSelectionChange"
        @on-sort-change="onSortChange"
        @on-row-click="onRowClick"
      />
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page
            :total="total"
            :current="current"
            :page-size="pageSize"
            @on-page-size-change="changePageSize"
            @on-change="changePage"
            :show-total="true"
            show-sizer
          ></Page>
        </div>
      </div>

    </Card>
  </div>
</template>

<script>
import { list, del, batchDelete } from '@/api/lky/lkyCollege'
import { createFormObj } from '@/libs/util'

import Add from './add.vue'
import Edit from './edit.vue'

export default {
  name: 'LkyCollege',
  components: {
    Add,
    Edit
  },
  data () {
    return {
      formLkyCollege: {
        id: '',
        name: '',
        collage_number: '',
        creation_time: '',
        update_time: ''
      },
      columns: [
        { type: 'selection', width: 60, align: 'center' },
        { key: 'name', title: '学院名称', sortable: true },
        // { key: 'collage_number', title: '学院编号', sortable: true },
        { key: 'remark_info', title: '备注信息', sortable: false },
        { key: 'creation_time', title: '创建时间', sortable: true },
        // { key: 'update_time', title: '更新时间', sortable: true },
        {
          title: '操作',
          key: 'action',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editLkyCollege = params.row
                      this.handleOpenPage('edit')
                    }
                  }
                },
                '编辑'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '提示',
                        content: '<p>确定删除吗?</p>',
                        onOk: () => {
                          del({ 'lkyCollege.id': params.row.id }).then(res => {
                            if (res) {
                              this.tableData.splice(params.index, 1)
							  this.$Message.success('删除成功')
                            }else{
								this.$Message.error('删除失败，本学院下还有专业')
							}
                          })
                        },
                        onCancel: () => {}
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableData: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isOpenAddPage: false,
      isOpenEditPage: false,
      editLkyCollege: {},
      selection: []
    }
  },
  methods: {
    handleSubmit (name) {
      // 查询按钮事件
      this.getLkyCollegeList(this.current, this.pageSize)
      this.$Message.success('Success!')
    },
    handleReset (name) {
      // 重置按钮事件
      this.$refs[name].resetFields()
    },
    handleOpenPage (name) {
      // 打开页面事件(添加页,编辑页...)
      if (name === 'add') this.isOpenAddPage = true
      else if (name === 'edit') {
        this.isOpenEditPage = true
      }
    },
    addLkyCollege (row) {
      this.tableData.push(row)
      this.getLkyCollegeList(this.current, this.pageSize)
    },
    updateLkyCollege (row) {
      this.$set(this.tableData, row._index, row)
    },
    batchDelete () {
      this.$Modal.confirm({
        title: '提示',
        content: '<p>确定删除吗?</p>',
        onOk: () => {
          if (this.selection.length > 0) {
            let ids = ''
            for (var i = 0; i < this.selection.length; i++) {
              ids += ',' + this.selection[i].id
            }
            batchDelete({ ids: ids }).then(res => {
              for (var i = 0; i < this.selection.length; i++) {
                this.tableData = this.tableData.filter(
                  item => item.id !== this.selection[i].id
                )
              }
            })
          } else {
            this.$Message.error('未选中记录!')
          }
        },
        onCancel: () => {}
      })
    },
    getLkyCollegeList (current, pageSize, paras) {
      let para = { pageNumber: current, pageSize: pageSize }
      if (paras) Object.assign(para, paras)

      list(this.handleRequestParams(para)).then(res => {
        this.tableData = res.list
        this.current = parseInt(res.pageNumber)
        this.pageSize = parseInt(res.pageSize)
        this.total = parseInt(res.totalRow)
      })
    },
    handleRequestParams (para) {
      Object.assign(para, createFormObj(this.formLkyCollege, 'lkyCollege'))
      return para
    },
    exportExcel () {
      this.$refs.tableLkyCollege.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      })
    },
    changePage (pageNumber) {
      this.getLkyCollegeList(pageNumber, this.pageSize)
    },
    changePageSize (pageSize) {
      this.getLkyCollegeList(this.current, pageSize)
    },
    remove (index) {
      this.data6.splice(index, 1)
    },
    onSelectionChange (selection) {
      this.selection = selection
    },
    onSortChange (obj) {
      if (obj.order === 'normal') {
        this.getLkyCollegeList(1, this.pageSize)
      } else {
        this.getLkyCollegeList(1, this.pageSize, {
          field: obj.key,
          order: obj.order
        })
      }
    },
    onRowClick (row, index) {
      console.log(row)
    }
  },
  mounted () {
    this.getLkyCollegeList(this.current, this.pageSize)
  }
}
</script>

<style>
</style>
